package com.maxd.service.Commodity;

import com.maxd.model.Commodity;
import com.maxd.respository.commodity.CommodityJpaRepository;
import com.maxd.respository.commodity.CommodityRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 商品
 */
@Service
public class CommodityServiceImpl implements CommodityService {

    /** 商品 */
    @Autowired
    private CommodityJpaRepository commodityJpaRepository;
    /** 商品 */
    @Autowired
    private CommodityRepository commodityRepository;


    @Override
    public Page<Commodity> selectCommodityList(Pageable pageable) {
        return commodityRepository.selectCommodityList(pageable);
    }

    @Override
    public Commodity selectById(Long id) {
        return commodityRepository.selectById(id);
    }

    @Override
    public Page<Commodity> selectByUserId(Long userId, Pageable pageable) {
        return commodityRepository.selectByUserId(userId,pageable);
    }

    @Override
    public Commodity save(Commodity commodity) {
        Commodity commodity1 = commodityRepository.selectCommodityList(commodity.getItemid());
        if(commodity1!=null){
            return null;
        }
        return commodityJpaRepository.save(commodity);
    }

    @Override
    public void saveAll(List<Commodity> list) {
        commodityJpaRepository.saveAll(list);
    }

    @Override
    public Commodity update(Commodity commodity) {
        return commodityJpaRepository.save(commodity);
    }

    @Override
    public void deleteById(Long id) {
        commodityJpaRepository.deleteById(id);
    }

    @Override
    @Transactional
    public void deleteByIdList(String ids) {
        String []id=ids.split(",");
        for(String i:id){
            commodityJpaRepository.deleteById(Long.parseLong(i));
        }

    }

}
